#include "sortdialog.h"
#include "ui_sortdialog.h"
#include <QSettings>

SortDialog::SortDialog(QWidget *parent) :
    QDialog(parent),  ui(new Ui::SortDialog) {
    ui->setupUi(this);
    QSettings s;
    m_path = QString();
    Option o = (Option) s.value("sorting").toInt();
    checkOption(o);
}

void SortDialog::checkOption(Option o) {
    switch (o) {
        case Album: ui->albumTrackNrButton->setChecked(true);
                    break;
        case Recent: ui->recentlyAddedButton->setChecked(true);
                    break;
        case Artist: ui->artistTitleButton->setChecked(true);
                    break;
        case Title:  ui->trackTitleButton->setChecked(true);
                    break;
        case None: ui->noneButton->setChecked(true);
                    break;
    }
}

SortDialog::SortDialog(QString path, QWidget *parent) :
    QDialog(parent), ui(new Ui::SortDialog) {
    ui->setupUi(this);
    m_path = path;
    QSettings s;
    Option o = (Option) s.value("sorting/" + path,
                s.value("sorting").toInt()).toInt();
    checkOption(o);
}


SortDialog::Option SortDialog::option() const {
    if (ui->buttonGroup->checkedButton() == ui->albumTrackNrButton) {
        return Album;
    } 
    if (ui->buttonGroup->checkedButton() == ui->recentlyAddedButton) {
        return Recent;
    } 
    if (ui->buttonGroup->checkedButton() == ui->artistTitleButton) {
        return Artist;
    } 
    if (ui->buttonGroup->checkedButton() == ui->trackTitleButton) {
        return Title;
    }
    return None;
}

void SortDialog::accept() {
    QSettings s;
    s.setValue("sorting", option());
    QDialog::accept();
}

SortDialog::~SortDialog() {
    delete ui;
}

void SortDialog::changeEvent(QEvent *e) {
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
